package handle

import (
	"encoding/json"
	"fmt"
	"net/http"
)

const (
	kPasswdMinLen = 6
	kPasswdMaxLen = 18
)

const (
	UserExist = 1 + iota
	UserNotExist
	UsernameIsNULL
	UsernameIsInvalid
	PasswdIsNULL
	PasswdIsInvalid
	PasswdIsDiff
	PhoneNoIsInvalid
	ChannelIsInvalid
	PlatformIsInvalid

	Success     = 0
	SystemError = -1
)

var ErrMessage map[int]string = map[int]string{
	UserExist:         "用户已存在",
	UserNotExist:      "用户不存在",
	UsernameIsNULL:    "用户不能为空",
	UsernameIsInvalid: "用户名仅为数字和字母",
	PasswdIsNULL:      "密码不能为空",
	PasswdIsInvalid:   "密码长度为6-18位",
	PasswdIsDiff:      "两次输入的密码不同",
	PhoneNoIsInvalid:  "无效的手机号码",
	ChannelIsInvalid:  "未知渠道",
	PlatformIsInvalid: "未知平台",
	SystemError:       "未知错误",
	Success:           "成功",
}

func NewResult() *Result {
	return &Result{Result: Success, Message: ErrMessage[Success]}
}

type Result struct {
	Result  int32  `json:"result"`
	Message string `json:"message"`
}

type LoginResp struct {
	Result
	SessionId string `json:"sid"`
}

func SendResp(resp interface{}, w http.ResponseWriter) {
	buff, err := json.Marshal(resp)
	if err != nil {
		fmt.Printf("json.Marshal error: ", err)
		return
	}

	fmt.Fprintln(w, string(buff))
}
